package com.chainyoung.trademarket.adapter;


import android.content.DialogInterface;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.bus.AppManagerEvent;
import com.chainyoung.common.bus.RxBus;
import com.chainyoung.common.event.BaseEvent;
import com.chainyoung.common.utils.ConvertUtils;
import com.chainyoung.common.view.CommonPopupWindow;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;
import com.chainyoung.trademarket.R;
import com.chainyoung.trademarket.entity.Msg;

import org.simple.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.chainyoung.common.bus.AppManagerEvent.APP_EXIT;

public class MessageListAdapter extends BaseQuickAdapter<Msg, BaseViewHolder> {

    SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private OnClickCancel onClickCancel;
    private String msgType;


    public MessageListAdapter(@Nullable List<Msg> data, String msgType) {
        super(R.layout.list_msg_item, data);
        this.msgType = msgType;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final Msg item) {

        if (msgType.equals("chat")) {
            helper.setText(R.id.tv_msg_title, item.getFrom_name() + String.format(mContext.getString(R.string.new_chat_message_sent_to_you), item.getNum()));
        } else if (msgType.equals("order")) {
            helper.setText(R.id.tv_msg_title, item.getContent());
        } else {
            helper.setText(R.id.tv_msg_title, item.getTitle());
        }
        helper.setTextColor(R.id.tv_msg_title, item.isIs_read() ? Color.parseColor("#7e8ca6") :
                Color.parseColor("#000000"));
        if (item.getAddtime() != null) {
            helper.setText(R.id.tv_time, sd.format(new Date(item.getAddtime() * 1000)));
        }
        helper.setTextColor(R.id.tv_time, item.isIs_read() ? Color.parseColor("#7e8ca6") :
                Color.parseColor("#4486f5"));
        if (msgType.equals("admin")) {
            helper.setText(R.id.tv_content, item.getIntro());
        }

        helper.getView(R.id.rl_msg).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setMessage(mContext.getString(R.string.confirm_del_msg))
                        .setPositiveButton(mContext.getString(com.chainyoung.component_tmperson.R.string.confirm), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (onClickCancel != null) {
                                    onClickCancel.onDelMsg(helper.getAdapterPosition(), item.getId());
                                }
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton(mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

                builder.create();
                builder.show();
                return true;
            }
        });

        helper.getView(R.id.rl_msg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickCancel != null) {
                    onClickCancel.onUpdateStatus(helper.getAdapterPosition(), item.getId(), item);
                }
            }
        });

    }


    public void setOnClickCancel(OnClickCancel onClickCancel) {
        this.onClickCancel = onClickCancel;
    }


    public interface OnClickCancel {
        void onDelMsg(int position, String id);

        void onUpdateStatus(int position, String id, Msg item);
    }


}

